package angels.client.Activities;
import com.readystatesoftware.mapviewballoons.R;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.app.ListActivity;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import angels.client.Entities.Categories;
import angels.client.Entities.Categories.Type;
import angels.client.Listeners.MainMenuListener;
import angels.client.Services.BusyHourService;

public class SubCategoryMenu extends Activity implements MainMenuListener, ServiceConnection{
	 
	private BusyHourService service = null;
    ListView spinSubcategory_list;
    Handler handler = new Handler();
    String selected_category;
    TextView txtViewHeader;
    
    public void onServiceConnected(ComponentName className, IBinder binder) {
		service = ((BusyHourService.LocalBinder) binder).getService();
	}

	public void onServiceDisconnected(ComponentName className) {
	    service = null;
	}
	
    @Override	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        spinSubcategory_list= new ListView(this);
        final Bundle extras = getIntent().getExtras();
        selected_category = extras.getString("category");
                
        spinSubcategory_list.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,int pos, long id) {
				String strSelctedSubCategory = parent.getItemAtPosition(pos).toString();
				//service.getCategories(SubCategoryMenu.this,Type.subcategory,strSelctedCategory);
           	 Intent intent = new Intent(getApplicationContext(), TrafficActivity.class);
           	 intent.putExtra("category", selected_category);
           	 intent.putExtra("subcategory", strSelctedSubCategory);
           	 startActivity(intent);
			}
		});
       
        LinearLayout layout = new LinearLayout(this);
        layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        layout.setOrientation(LinearLayout.VERTICAL);
        
        layout.setBackgroundColor(0xFFEEEEEE);
        
        txtViewHeader = new TextView(this);
        txtViewHeader.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        txtViewHeader.setGravity(Gravity.CENTER_HORIZONTAL);
        txtViewHeader.setText(selected_category);
        txtViewHeader.setTextColor(0xFF333333);
        txtViewHeader.setTextSize(24);
        txtViewHeader.setShadowLayer((float) 0.5, 2, 2, 0xFFEEEEEE);
        txtViewHeader.setBackgroundColor(0xFFCCCCCC);
        layout.addView(txtViewHeader);
        
        layout.addView(spinSubcategory_list);
        layout.setClickable(false);
        layout.setFocusable(false);
        setContentView(layout);
        
        String[] subCategories = extras.getStringArray("subcategory");
        
        Intent ServiceGps = new Intent(this, BusyHourService.class);
        getApplicationContext().bindService(ServiceGps, this, Context.BIND_AUTO_CREATE);
        
        ArrayAdapter<String> catArrayAdapter = new ArrayAdapter<String>(this,
        		android.R.layout.select_dialog_item,subCategories);
        spinSubcategory_list.setAdapter(catArrayAdapter);
        //spinSubcategory_list.setBackgroundColor(Color.LTGRAY);
    }
    
    @Override
	public void onCategoriesReceived(Categories subcategories1) {
    	String[] subcategories=subcategories1.getCategories();
    	final ArrayAdapter<String> subArrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line,subcategories);
    	handler.post(new Runnable() {	
            public void run() {
            	spinSubcategory_list.setAdapter(subArrayAdapter);
            }
        });
	}
	
	public void onCategoriesReceivedError()
	{
	}
}